package org.youchan.aco.model;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class Bean {
	protected static List<Field> fieldList;
	
	protected static void initialize(Class<?> cls) {
		fieldList = new ArrayList<Field>();
		Field[] fields = cls.getDeclaredFields();
		for (Field field : fields) {
			Property p = field.getAnnotation(Property.class);
			if (p != null) {
				fieldList.add(field);
			}
		}
	}
	
	public Object get(String name) throws IllegalAccessException {
		for (Field field : fieldList) {
			if (field.getName().equals(name)) {
				return field.get(this);
			}
		}
		
		throw new IllegalAccessException("Bean doesn't have a field '" + name + "'");
	}
	
	public void set(String name, Object value) throws IllegalAccessException {
		for (Field field : fieldList) {
			if (field.getName().equals(name)) {
				field.set(this, value);
				return;
			}
		}
		
		throw new IllegalAccessException("Bean doesn't have a field '" + name + "'");
	}
}
